package com.pie4cloud.pie.job.api.utils;


import com.alibaba.cloud.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public  class RegUtils {


    public static String[] match(String reg, String analysisParam) throws RegException {
        if (StringUtils.isEmpty(reg) || StringUtils.isEmpty(analysisParam)) {
            throw new RegException("reg is null or regData is null");

        }
        Pattern compile = Pattern.compile(reg);
        Matcher matcher = compile.matcher(analysisParam);
        if (!matcher.matches()) {
            throw new RegException("reg not matches ");
        }
        int i = matcher.groupCount();
        String[] result = new String[i];
        for (int y = 1; y <= i; y++) {
            result[y - 1] = matcher.group(y);
        }
        return result;
    }

    public static Map<String,String> matchToMap(String reg, String analysisParam) throws RegException {
    	Map<String,String> map = new HashMap<>();
		String[] match = match(reg, analysisParam);
		for (int i = 0; i < match.length; i++) {
			map.put(i+"",match[i]);
		}
		return map;
	}

	public static boolean checkMatch(String reg, String analysisParam) throws RegException {
		if (StringUtils.isEmpty(reg) || StringUtils.isEmpty(analysisParam)) {
			throw new RegException("reg is null or regData is null");

		}
		Pattern compile = Pattern.compile(reg);
		Matcher matcher = compile.matcher(analysisParam);
		return matcher.matches();
	}

    public static void main(String[] args) throws RegException {


//		int i = StringUtils.indexOf("Z_SATE_C_(BAWX)_.*?_P_(FY3D)_(GNOSX)_(GBAL)_(L1)_(20220109)_.*?_IEG08_MS.NC", "(");
//		System.out.println(i);
		String tstREg="Z_SATE_C_(BAWX)_.*?_P_(FY3D)_(GNOSX)_(GBAL)_(L1)_(\\d{8})_.*?_IEG08_MS.NC";
		String ACtiRe="Z_SATE_C_BAWX_20220109235921_P_FY3D_GNOSX_GBAL_L1_20220109_2113_IEG08_MS.NC";

		String reg  = "/CMADAAS/DATA/SURF/MERGE/CMPA/FAST/(HOR|3HOR)/(2021)/(20210723)/Z_SURF_C_BABJ_(\\d{14})_P_CMPA_FAST_CHN_(0P05)_(HOR|3HOR)-PRE-(\\d{10}).GRB2";
		String text = "/CMADAAS/DATA/SURF/MERGE/CMPA/FAST/HOR/2021/20210723/Z_SURF_C_BABJ_20210723000438_P_CMPA_FAST_CHN_0P05_HOR-PRE-2021072300.GRB2";
		String[] match = match(reg, text);
		for (String m:
			 match) {
			System.out.println(m);
		}
	}
}
